package com.woniu.cq.sentinel;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * @author Daniel
 * @version 1.0.0
 * @ClassName MyBlockHandler
 * @Description TODO
 * @createTime 2020年09月21日 17:02:00
 */
@Component
public class MyBlockHandler implements BlockExceptionHandler {
    private ObjectMapper om = new ObjectMapper();
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",500);
        map.put("msg","超过阈值");
        map.put("error",e);
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json;charset=utf-8");
        om.writeValue(httpServletResponse.getOutputStream(),map);
    }
}
